#!/usr/bin/perl -w
# calendarEncrypt.pl - encrypt/decrypt/shred files
use strict;

die "specify a mode " unless @ARGV == 1;
my $mode = $ARGV[0];

chomp(my $userName = `whoami`);

if( $mode eq "encrypt" )
{
  my $res = `gpg --yes --armor --encrypt -r $userName /tmp/calendarEvent`;
  $res = `shred /tmp/calendarEvent; rm /tmp/calendarEvent`;

}elsif( $mode eq "decrypt" )
{

  open(INFILE,"/tmp/calendarEvent.temp") or die "no in file";
  open(OUTFILE,"> /tmp/calendarEvent.encrypted" ) or die "no file out";

    while(my $line =<INFILE>)
    {
      my $begin = substr( $line, 0, 23 );
      print OUTFILE "-----$begin\n";

      my $version = substr( $line, 23, 34 );
      print OUTFILE "$version\n";

      print OUTFILE "\n";

      my $body = substr( $line, 57 );
      $body = substr($body, 0, length($body)-26);

      my @parts = split " ", $body;
      for my $piece( @parts )
      {
        print OUTFILE "$piece\n";
      }

      print OUTFILE "-----END PGP MESSAGE-----\n";
    }#while each line

  close(OUTFILE);
  close(INFILE);

  my $cmd  = qq{ gpg --yes --decrypt /tmp/calendarEvent.encrypted };
     $cmd .= qq{ > /tmp/calendarEvent.decrypted };
  my $res = `$cmd`;
}elsif( $mode eq "shred" )
{
  #my $res = `shred /tmp/calendarEvent.decrypted`;
  #$res    = `rm /tmp/calendarEvent.decrypted`;
}

# EOF
